﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Qurihara;

namespace KotodamaPlugin.MultiViewSnapPlugin
{
	/// <summary>
	/// ViewEditForm の概要の説明です。
	/// </summary>
	public class ViewEditForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonDelete;
		private System.Windows.Forms.Button buttonUp;
		private System.Windows.Forms.Button buttonDown;
		private System.Windows.Forms.TextBox textBoxName;
		private System.Windows.Forms.Label labelName;
		private System.Windows.Forms.ListBox listBoxView;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label1;
		public System.Windows.Forms.Button buttonListDel;
		public System.Windows.Forms.Button buttonListNew;
		public System.Windows.Forms.ComboBox comboBoxListName;

		private ViewSnapList viewSnapList;
		public ViewSnapList ViewSnapList
		{
			get{return viewSnapList;}
			set{viewSnapList = value;}
		}
		public ViewEditForm()
		{
			InitializeComponent();

            if (Qurihara.QBasic.Util.IsPropertyTrue("Base.English"))
            {
                this.Text = "Edit views";
                buttonDelete.Text = "Del.";
                labelName.Text = "View";
                textBoxName.Text = "Name";
                label1.Text = "List";
                buttonListNew.Text = "New list";
                buttonListDel.Text = "Del. list";

            }
		}
		public ViewEditForm(ViewSnapList vsl)
		{
			InitializeComponent();

			viewSnapList = vsl;
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows フォーム デザイナで生成されたコード 
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonDelete = new System.Windows.Forms.Button();
			this.buttonUp = new System.Windows.Forms.Button();
			this.buttonDown = new System.Windows.Forms.Button();
			this.textBoxName = new System.Windows.Forms.TextBox();
			this.labelName = new System.Windows.Forms.Label();
			this.listBoxView = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.buttonListDel = new System.Windows.Forms.Button();
			this.buttonListNew = new System.Windows.Forms.Button();
			this.comboBoxListName = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// buttonDelete
			// 
			this.buttonDelete.Location = new System.Drawing.Point(0, 0);
			this.buttonDelete.Name = "buttonDelete";
			this.buttonDelete.Size = new System.Drawing.Size(40, 23);
			this.buttonDelete.TabIndex = 0;
			this.buttonDelete.Text = "削除";
			this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
			// 
			// buttonUp
			// 
			this.buttonUp.Location = new System.Drawing.Point(40, 0);
			this.buttonUp.Name = "buttonUp";
			this.buttonUp.Size = new System.Drawing.Size(40, 23);
			this.buttonUp.TabIndex = 1;
			this.buttonUp.Text = "↑";
			this.buttonUp.Click += new System.EventHandler(this.buttonUp_Click);
			// 
			// buttonDown
			// 
			this.buttonDown.Location = new System.Drawing.Point(80, 0);
			this.buttonDown.Name = "buttonDown";
			this.buttonDown.Size = new System.Drawing.Size(40, 23);
			this.buttonDown.TabIndex = 2;
			this.buttonDown.Text = "↓";
			this.buttonDown.Click += new System.EventHandler(this.buttonDown_Click);
			// 
			// textBoxName
			// 
			this.textBoxName.Location = new System.Drawing.Point(40, 24);
			this.textBoxName.Name = "textBoxName";
			this.textBoxName.Size = new System.Drawing.Size(80, 19);
			this.textBoxName.TabIndex = 3;
			this.textBoxName.Text = "名前";
			this.textBoxName.TextChanged += new System.EventHandler(this.textBoxName_TextChanged);
			// 
			// labelName
			// 
			this.labelName.Location = new System.Drawing.Point(8, 32);
			this.labelName.Name = "labelName";
			this.labelName.Size = new System.Drawing.Size(32, 16);
			this.labelName.TabIndex = 4;
			this.labelName.Text = "名前";
			// 
			// listBoxView
			// 
			this.listBoxView.ItemHeight = 12;
			this.listBoxView.Location = new System.Drawing.Point(0, 48);
			this.listBoxView.Name = "listBoxView";
			this.listBoxView.ScrollAlwaysVisible = true;
			this.listBoxView.Size = new System.Drawing.Size(120, 280);
			this.listBoxView.TabIndex = 5;
			this.listBoxView.SelectedIndexChanged += new System.EventHandler(this.listBoxView_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 336);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 7;
			this.label1.Text = "リスト名";
			// 
			// buttonListDel
			// 
			this.buttonListDel.Location = new System.Drawing.Point(56, 352);
			this.buttonListDel.Name = "buttonListDel";
			this.buttonListDel.Size = new System.Drawing.Size(64, 23);
			this.buttonListDel.TabIndex = 9;
			this.buttonListDel.Text = "リスト削除";
			// 
			// buttonListNew
			// 
			this.buttonListNew.Location = new System.Drawing.Point(0, 352);
			this.buttonListNew.Name = "buttonListNew";
			this.buttonListNew.Size = new System.Drawing.Size(56, 23);
			this.buttonListNew.TabIndex = 8;
			this.buttonListNew.Text = "新リスト";
			// 
			// comboBoxListName
			// 
			this.comboBoxListName.ItemHeight = 12;
			this.comboBoxListName.Location = new System.Drawing.Point(40, 328);
			this.comboBoxListName.Name = "comboBoxListName";
			this.comboBoxListName.Size = new System.Drawing.Size(80, 20);
			this.comboBoxListName.TabIndex = 6;
			this.comboBoxListName.Text = "-";
			// 
			// ViewEditForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(120, 372);
			this.Controls.Add(this.buttonListDel);
			this.Controls.Add(this.buttonListNew);
			this.Controls.Add(this.comboBoxListName);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.listBoxView);
			this.Controls.Add(this.labelName);
			this.Controls.Add(this.textBoxName);
			this.Controls.Add(this.buttonDown);
			this.Controls.Add(this.buttonUp);
			this.Controls.Add(this.buttonDelete);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "ViewEditForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "視点の編集";
			this.TopMost = true;
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ViewEditForm_Closing);
			this.ResumeLayout(false);

		}
		#endregion

		public void UpdateList()
		{
			this.listBoxView.Items.Clear();
			foreach(ViewSnap vs in viewSnapList)
			{
				this.listBoxView.Items.Add(vs);
			}
			if (this.listBoxView.Items.Count > 0)
			{
				//this.listBoxView.SelectedIndex = 0;
				//this.textBoxName.Text = ((ViewSnap)viewSnapList[0]).Name;
			}
			viewSnapList.Update();
		}
		private void ViewEditForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			this.Hide();
			e.Cancel = true;
		}

		private void buttonDelete_Click(object sender, System.EventArgs e)
		{
			if(this.listBoxView.SelectedItems.Count > 0)
			{
				int indexDeleted=0;
				foreach(ViewSnap vs in this.listBoxView.SelectedItems)
				{
					this.viewSnapList.Remove(vs);
					vs.Dispose();
					indexDeleted = this.listBoxView.Items.IndexOf(vs);
				}
				UpdateList();
				if (this.listBoxView.Items.Count > indexDeleted)
					this.listBoxView.SelectedIndex = indexDeleted;
			}
		}

		private ArrayList workList = new ArrayList();
		private void buttonUp_Click(object sender, System.EventArgs e)
		{
			if(this.listBoxView.SelectedItems.Count > 0)
			{
				int top = this.listBoxView.Items.Count-1;
				foreach(ViewSnap vs in this.listBoxView.SelectedItems)
				{
					int ind = this.listBoxView.Items.IndexOf(vs);
					if (top > ind) top = ind;
				}
				if (top > 0)
				{
					workList.Clear();
					//二つ以上に分割されているセットは、まとめて一つ上に送る。
					foreach(ViewSnap vs in this.listBoxView.SelectedItems)
					{
						workList.Add(vs);
					}
					foreach(ViewSnap vs in workList)
					{
						this.viewSnapList.Remove(vs);
						//this.listBoxView.Items.Remove(vs);
					}
					for(int i = 0;i<workList.Count;i++)
					{
						this.viewSnapList.Insert(top-1+i,workList[i]);
						//this.listBoxView.Items.Insert(top-1+i,workList[i]);
					}
				}
				UpdateList();
				if (this.listBoxView.Items.Count > top-1 && top-1 >= 0)
					this.listBoxView.SelectedIndex = top-1;
			}
		}

		private void buttonDown_Click(object sender, System.EventArgs e)
		{
			if(this.listBoxView.SelectedItems.Count > 0)
			{
				int bottom = 0;
				foreach(ViewSnap vs in this.listBoxView.SelectedItems)
				{
					int ind = this.listBoxView.Items.IndexOf(vs);
					if (bottom < ind) bottom = ind;
				}
				if (bottom < this.listBoxView.Items.Count-1)
				{
					ViewSnap beforeInsert = (ViewSnap)listBoxView.Items[bottom+1];
					workList.Clear();
					//二つ以上に分割されているセットは、まとめて一つ下に送る。
					foreach(ViewSnap vs in this.listBoxView.SelectedItems)
					{
						workList.Add(vs);
					}
					foreach(ViewSnap vs in workList)
					{
						this.viewSnapList.Remove(vs);
						//this.listBoxView.Items.Remove(vs);
					}
					int insertPoint = this.listBoxView.Items.IndexOf(beforeInsert);
					for(int i = 0;i<workList.Count;i++)
					{
						this.viewSnapList.Insert(insertPoint+i,workList[i]);
						//this.listBoxView.Items.Insert(insertPoint+i,workList[i]);
					}
				}
				UpdateList();
				if (this.listBoxView.Items.Count > bottom+1 && bottom+1 >= 0)
					this.listBoxView.SelectedIndex = bottom+1;
			}
		}

		bool selectedChanged = false;
		private void listBoxView_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			selectedChanged = true;
			this.textBoxName.Text = ((ViewSnap)viewSnapList[this.listBoxView.SelectedIndex]).Name;
			((ViewSnap)viewSnapList[this.listBoxView.SelectedIndex]).Warp();
		}

		private void textBoxName_TextChanged(object sender, System.EventArgs e)
		{
			if(listBoxView.SelectedItem != null && !selectedChanged)
			{
				object obj = listBoxView.SelectedItem;
				ViewSnap vs = (ViewSnap)listBoxView.SelectedItem;
				vs.Name = textBoxName.Text;
				UpdateList();
				this.listBoxView.SelectedItem = obj;
			}
			selectedChanged = false;
		}
	}
}
